package me.mingshan.leetcode;

/**
 * @author hanjuntao
 * @date 2025/6/9 0009
 */
public class L_557_reverse_words_in_a_string_iii {

    public static void main(String[] args) {
        String s = "Let's take LeetCode contest";
        System.out.println(reverseWords(s));
    }

    /**
     * 按照 “ ”分割字符串，每个单独翻转
     *
     * @param s
     * @return
     */
    public static String reverseWords(String s) {
        String[] split = s.split(" ");
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < split.length; i++) {
            String s1 = split[i];
            result.append(reverse(s1));
            if (i != split.length - 1) {
                result.append(" ");
            }
        }

        return result.toString();
    }

    private static char[] reverse(String s1) {
        char[] chars = s1.toCharArray();


        int left = 0;
        int right = chars.length - 1;

        while (left < right) {
            char temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;

            left++;
            right--;
        }

        return chars;
    }
}
